﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using thousandclear.Data;
using thousandClear.Areas.Admin.Models;
using thousandClear.Util.Media;
using thousandClear.Util.Setting;

namespace thousandClear.Areas.Admin.Controllers
{
    public class ProjectController : BaseController
    {
        // GET: Admin/Project
        public ActionResult Index(int? page, int? count)
        {
            string level = "1";
            level = GetCookie("level");
            ViewBag.level = level;
            page = page ?? 1;
            count = count ?? 10;
            int totalCount = 0;
            int totalPage = 1;
            int currentPage = page.Value;
            int pageCount = count.Value;
            List<ProjectModel> list = new List<ProjectModel>();
            using (var db = new qianjingEntities())
            {
                var query = db.D_item.AsQueryable().Where(a => a.RowStatus == 1);
                totalCount = query.Count();
                var records = query.OrderByDescending(a => a.DIMID).Skip((page.Value - 1) * count.Value).Take(count.Value).ToList();
               
                foreach (var item in records)
                {
                    list.Add(new ProjectModel
                    {
                        DIMID = Convert.ToInt16(item.DIMID),
                        dimDesc = item.dimDesc,
                        dimName=item.dimName,
                        dimType = item.dimType,
                        dimTotalRate = Convert.ToDecimal(item.dimTotalRate),
                    });
                }

            }


            ViewBag.TotalCount = totalCount;
            totalPage = int.Parse((totalCount / count).ToString()) + (totalCount % count == 0 ? 0 : 1);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            ViewBag.PageCount = pageCount;

            return View(list);
        }
        //HttpGet: Admin/Aunt
        [HttpGet]
        public ProjectControllerResponse remove(long id)
        {
            ProjectControllerResponse resp = new ProjectControllerResponse();

            using (var db = new qianjingEntities())
            {

                var query = db.D_item.FirstOrDefault(a => a.DIMID == id);

                if (query == null)
                {
                    resp.ErrMsg = "该记录不存在";
                    return resp;
                }
                query.RowStatus = 2;

                db.SaveChanges();
            }
            return resp;


        }
        // GET: Project/Add
        public ActionResult Add()
        {
            return View();
        }
        // POST: Project/Add
        [HttpPost]
        public ActionResult Add(string data)
        {

            AppSetting setting = new AppSetting();
            ImageResolver resolver = new ImageResolver();
            var post = JObject.Parse(data);
            string dimTotalRate= post["dimTotalRate"].Value<string>();
            string ProjectName = post["ProjectName"].Value<string>();
            string ProjectDesc = post["ProjectDesc"].Value<string>();
            int ProjectType = post["ProjectType"].Value<int>();

            using (var db = new qianjingEntities())
            {

                db.D_item.Add(new D_item
                {
                    dimName = ProjectName,
                    dimDesc = ProjectDesc,
                    dimType = ProjectType,
                    dimTotalRate= Convert.ToDecimal(dimTotalRate),
                    RowStatus = 1,
                    CreatedDate = DateTime.Now
                });
                db.SaveChanges();
            }

            return RedirectToAction("Add");

        }
        // POST: Project/Edit
        [HttpPost]
        public ActionResult Edit(string data)
        {
            AppSetting setting = new AppSetting();
            ImageResolver resolver = new ImageResolver();

            var post = JObject.Parse(data);
            int DIMID = post["DIMID"].Value<int>();
            string ProjectName = post["ProjectName"].Value<string>();
            string ProjectDesc = post["ProjectDesc"].Value<string>();
            int ProjectType = post["ProjectType"].Value<int>();
            string dimTotalRate = post["dimTotalRate"].Value<string>();
            
          
            using (var db = new qianjingEntities())
            {
                var query = db.D_item.FirstOrDefault(a => a.DIMID == DIMID);
                query.dimDesc = ProjectDesc;
                query.dimName = ProjectName;
                query.dimType = ProjectType;             
                query.UpdatedDate = DateTime.Now;
                query.dimTotalRate = Convert.ToDecimal(dimTotalRate);
                db.SaveChanges();
            }

            return RedirectToAction("Index");

        }

        // GET: Project/Edit
        public ActionResult Edit(int id)
        {
            ProjectModel model = new ProjectModel();
            using (var db = new qianjingEntities())
            {
                var query = db.D_item.FirstOrDefault(a => a.DIMID == id);
                if (query != null)
                {
                    model.DIMID = Convert.ToInt16(query.DIMID);
                    model.dimDesc = query.dimDesc;
                    model.dimType = query.dimType;
                    model.dimName = query.dimName;                 
                    model.dimTotalRate = Convert.ToDecimal(query.dimTotalRate);
                }
            }
            return View(model);
        }
    }
}